********************************************************************************************
*DID at the canton level: cadaster progress --> communal land administration; public works *
********************************************************************************************

cd "~"
cd "~\Dropbox\Research\Research projects\Cadastre\Data\analysis final\replication"

**# 1. Communal administration
**********************************************************

use ".\data\data_canton.dta", clear

drop if bienscomt==.
drop if year>1848
drop if mindateref<1807

*drop if mindateref==9999 /*Important! otherwise, main comparison group (never treated)*/
*We know that all missing cantons are cantons which were NOT centrally cadastered (not on Hennet's map)
*--> keep them for central cadaster evaluation
*drop if mindateref==.

egen cantidn = group(cantid)

gen lsharepop2k = log(1+shareurbanpopintcant)
foreach var in distpref distspref distparis{
	gen l`var'=log(1+`var')
}

*Controls x time trend
cap drop inter*
foreach var in rugged wheat distparis distpref distspref distroads distforest areacant {
	gen inter`var'=l`var'*year
}

replace cadcent=(year>=mindateref & mindateref<=1822)

replace cadloc=(year>=mindateref & mindateref>1821)
replace cad=(year>=mindateref)
*Important! otherwise the missing cantons are the main comparison group
replace cadloc=. if mindateref==9999
replace cad=. if mindateref==9999

global CONTROLS inter* lpopintcant lsharepop2k rebelcant nbankint

** Event studies **

foreach var in bienscomt{
	*Baseline
	did_multiplegt_dyn `var' cantidn year cad, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all") 
	did_multiplegt_dyn `var' cantidn year cadcent, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_central") 
	did_multiplegt_dyn `var' cantidn year cadloc if mindateref>1821, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_loconly") 

	*No admin capital
	preserve
	drop if sousprefcant==1
	did_multiplegt_dyn `var' cantidn year cad, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all_nosouspref") 
	did_multiplegt_dyn `var' cantidn year cadcent, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_central_nosouspref") 
	did_multiplegt_dyn `var' cantidn year cadloc if mindateref>1821, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_loconly_nosouspref") 
	restore
	
	*Dept trend
	did_multiplegt_dyn `var' cantidn year cad, trends_nonparam(code_dept) controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all_deptrends") 
	did_multiplegt_dyn `var' cantidn year cadcent, trends_nonparam(code_dept) controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_central_deptrends") 
	did_multiplegt_dyn `var' cantidn year cadloc if mindateref>1821, trends_nonparam(code_dept) controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_loconly_deptrends") 

}

**# 2. Batiments civils  **
***************************

*All adopted projects
use ".\data\data_canton.dta", clear

drop if year>1840
drop if mindateref<1807

*Generate extensive margin variables
gen anyadoptedbatciv=(nadoptedbatciv>0)
gen anyadoptedbatcivlarge=(nadoptedbatcivlarge>0)
egen cantidn = group(cantid)
gen lsharepop2k = log(1+shareurbanpopintcant)

foreach var in distparis distpref distspref  {
	gen l`var'=log(1+`var')
}
cap drop inter*
foreach var in rugged wheat distparis distpref distspref distroads distforest areacant {
	gen inter`var'=l`var'*year
}

replace cadcent=(year>=mindateref & mindateref<=1822)
replace cadloc=(year>=mindateref & mindateref>1821)
*Important! otherwise the missing cantons are the main comparison group
replace cadloc=. if mindateref==9999
replace cad=. if mindateref==9999

global CONTROLS inter* lpopintcant lsharepop2k nbankint rebelcant


** Event studies **
foreach var in anyadoptedbatciv anyadoptedbatcivlarge{
	*Baseline
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all") 
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999 & year>1805 & year<1822, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_18061820") 
	did_multiplegt_dyn `var' cantidn year cad if year>1821 & cadcent==0, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_post21") 
	
	*No admin capital
	preserve
	drop if sousprefcant==1
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all_nosouspref") 
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999 & year>1805 & year<1822, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_18061820_nosouspref") 
	did_multiplegt_dyn `var' cantidn year cad if year>1821 & cadcent==0, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_post21_nosouspref") 
	restore
	
	*Only admin capital
	preserve
	keep if sousprefcant==1
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all_souspref")
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999 & year>1805 & year<1822, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_18061820_souspref") 
	did_multiplegt_dyn `var' cantidn year cad if year>1821 & cadcent==0, controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_post21_souspref") 
	restore

	*Dept trend
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999, trends_nonparam(code_dept) controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_all_deptrends") 
	did_multiplegt_dyn `var' cantidn year cad if mindateref!=9999 & year>1805 & year<1822, trends_nonparam(code_dept) controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_18061820_deptrends") 
	did_multiplegt_dyn `var' cantidn year cad if year>1821 & cadcent==0, trends_nonparam(code_dept) controls($CONTROLS) cluster(code_dept) effects(15) placebo(7) save("./stata outputs/`var'_post21_deptrends") 
}

